import java.awt.Image;
import java.sql.Time;
import java.util.Date;
import java.util.Random;
import java.util.Timer;
import java.util.concurrent.CountDownLatch;

import javax.swing.ImageIcon;
import javax.swing.JLabel;

public class Poultry extends JLabel implements Runnable {
	private static final long serialVersionUID = 1L;
	public int fps = 1000;
	public Thread game;
	public int x = 0, y = 0;
	public int speed = 1;
	public boolean running = false;
	public String egg = "/Images/egg.png";
	public String shit = "/Images/shit.png";
	public JLabel EggShit;
	public Background bg;
	public Date date;
	Random ran;
	

	public Poultry(String img, Background bg) {
		this.bg = bg;
		running = true;
		game = new Thread(this);
		game.start();
		this.setIcon(new ImageIcon(getClass().getResource(img)));
		this.setBounds(x, y, 82, 88);
		this.EggShit = new JLabel();
		
		ran = new Random();

	}

	public void getImage(String img, int w, int h) {
		xx = x;
		yy = 90;
		EggShit.setIcon(ScaleImage(img, w, h));
		EggShit.setBounds(xx, yy, w, h);
		bg.add(EggShit);
		bg.repaint();
	}

	public ImageIcon ScaleImage(String img, int w, int h) {
		ImageIcon ii = new ImageIcon(getClass().getResource(img));
		Image i = ii.getImage();
		return new ImageIcon(i.getScaledInstance(w, h, Image.SCALE_SMOOTH));
	}

	public void update() {
		if (x == 0)
			speed = 1;
		if (this.getBounds().getMaxX() == Main.width)
			speed = -1;
		x += speed;
		this.setBounds(x, y, 82, 88);
		getFPS();

	}

	int i = -1;
	int k = 1;
	int xx, yy;
	boolean isEgg = true;
	boolean flag = false;
	
	@Override
	public void run() {
		while (running) {

			if (flag) {
				MoveEgg();
				if (CheckIn()) {
					EggShit.setIcon(null);
					if (isEgg) {
						Main.point++;
						
					} else {
						Main.shitNum++;
						Main.eatShit=true;
					}
					flag = false;
				}
				if (CheckOut()) {
					if (isEgg) {
						Main.eggNum++;
						bg.EnableEgg();
						EggShit.setIcon(null);
						Main.MissEgg=true;
						flag = false;
						
					} 
				}
			}
			update();
			date = new Date();
			int time = date.getSeconds();
			if (time != i) {
				i = time;
				if (k++ % 5 == 0) {
					int r = ran.nextInt(3);
					if (r == 0) {
						getImage("/Images/shit.png", 41, 44);
						isEgg = false;
					} else {
						getImage("/Images/egg.png", 41, 50);
						isEgg = true;
					}
					flag = true;
					k = 1;

				}
			}
		}
	}

	public void MoveEgg() {
		EggShit.setBounds(xx, yy += Main.point / 20 + 1, 41, 50);
	}

	public boolean CheckIn() {
		int eggX1 = (int) EggShit.getBounds().getMinX();
		int eggX2 = (int) EggShit.getBounds().getMaxX();
		int roX1 = (int) bg.Ro.getBounds().getMinX();
		int roX2 = (int) bg.Ro.getBounds().getMaxX();
		int eggY1 = (int) EggShit.getBounds().getMinY();
		int eggY2 = (int) EggShit.getBounds().getMaxY();
		int roY1 = (int) bg.Ro.getBounds().getMinY();
		int roY2 = (int) bg.Ro.getBounds().getMaxY();
		if (eggX1 >= roX1 && eggX1 <= roX2 && eggX2 >= roX1 && eggX2 <= roX2
				&& eggY1 >= roY1 && eggY1 <= roY2 && eggY2 >= roY1
				&& eggY2 <= roY2) {
			return true;
		}
		return false;
	}

	public boolean CheckOut() {		
		int eggY1 = (int) EggShit.getBounds().getMinY();
		if (eggY1 > Main.height) {
			return true;
		}
		return false;
	}

	public void getFPS() {
		try {
			game.sleep(fps / 500);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void stop() {
		running = false;
		try {
			this.game.join();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
